package com.yanqu.road.server.cmd.union.cross.trade;

import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.pb.union.UnionTradeProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.DataAnalyticsMgr;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;
import io.netty.channel.ChannelHandlerContext;

import java.text.MessageFormat;

@Cmd(code = Protocol.S_UNION_TRADE_ROUTE_REWARD_FROM_CROSS, desc = "路线信息奖励")
public class RouteFinishMailRewardSyncCmdFromCross implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {

        UnionTradeProto.RouteFinishMailRewardSyncMsg rewardSyncMsg = UnionTradeProto.RouteFinishMailRewardSyncMsg.parseFrom(packet.getBytes());

        UserInfo userInfo = UserMgr.getUserInfoInRAM(rewardSyncMsg.getUserId());
        if(userInfo != null){

            String title = MultipleLanguageMgr.getContent(MailManager.UNION_TRADE_NORMAL_REWARD_SEND_TITLE, userInfo.getLanguage());
            String content = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.UNION_TRADE_NORMAL_REWARD_SEND_CONTENT, userInfo.getLanguage()),
                    MultipleLanguageMgr.getContent(rewardSyncMsg.getRouteName(), userInfo.getLanguage()));
            MailManager.sendMail(userInfo.getUserId(), eMailType.DAILY.getValue(), rewardSyncMsg.getReward(), content, title);

            if(rewardSyncMsg.getRewardType() == 0){
                DataAnalyticsMgr.trackOreWarGetRoadReward(userInfo.getUserId(), rewardSyncMsg.getRouteId(), rewardSyncMsg.getReward());
            }
        }

    }
}
